package com.radiant.service;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.UriBuilder;

import com.google.inject.Inject;
import com.radiant.data.entity.DevelopmentTrack;
import com.radiant.data.entity.DevelopmentTrack.MilestoneType;
import com.radiant.data.entity.Child;
import com.radiant.data.entity.DevelopmentTrackDAO;
import com.radiant.data.entity.Month;
import com.radiant.data.entity.DevelopmentTrack.Height;
import com.radiant.data.entity.DevelopmentTrack.Weight;

@Path("/monthlyTracker")
public class MonthlyTrackerServiceImpl implements MonthlyTrackerService {

	@Inject
	private DevelopmentTrackDAO developmentTrackDAO;

	public MonthlyTrackerServiceImpl() {
		super();
	}
	
	public List<String> getProtectedPaths() {
		List<String> paths = new ArrayList<String>();
		UriBuilder builder = UriBuilder.fromResource(this.getClass());
		paths.add(builder.path("*").build().toString());
		return paths;
	}

	public void createDefaultDevelopmentTracks(String childId) {
		for (Month month: Month.values()) {
			DevelopmentTrack dt = developmentTrackDAO.findByChildAndMonth(Child.DEFAULT_CHILD_NAME, month);
			if (dt != null) {
				DevelopmentTrack newTrack = new DevelopmentTrack();
				newTrack.setChildId(childId);
				newTrack.setShould(dt.getShould());
				newTrack.setMay(dt.getMay());
				newTrack.setProbable(dt.getProbable());
				newTrack.setEat(dt.getEat());
				newTrack.setSleep(dt.getSleep());
				developmentTrackDAO.save(newTrack);
			}
		}
	}

	@GET @Path("/child/{childId}/month/{month}")
	@Produces("application/json")
	public DevelopmentTrack findDevelopmentTrack(
			@PathParam("childId") String childId, 
			@PathParam("month") Month month) {
		return developmentTrackDAO.findByChildAndMonth(childId, month) ;
	}

	@POST @Path("/track/{trackId}/updateMilestones")
	@Consumes("application/x-www-form-urlencoded")
	public void updateMilestones(
			@PathParam("trackId") String developmentTrackId,
			@FormParam("milestoneType") MilestoneType milestoneType,
			MultivaluedMap<String, String> formParams) {

		DevelopmentTrack developmentTrack = developmentTrackDAO.findByPrimaryKey(developmentTrackId, false);
		
		if (developmentTrack != null) {
			developmentTrack.updateTrackItems(formParams, milestoneType);
			developmentTrackDAO.save(developmentTrack);
		}
	}

	@POST @Path("/track/{trackId}/updateHeight")
	@Consumes("application/x-www-form-urlencoded")
	public void updateHeight(
			@PathParam("trackId") String developmentTrackId, 
			@FormParam("height") float height, 
			@FormParam("inches") boolean inches) {
		DevelopmentTrack developmentTrack = developmentTrackDAO.findByPrimaryKey(developmentTrackId, false);
		if (developmentTrack != null) {
			developmentTrack.setHeight(new Height(height, inches));
			developmentTrackDAO.save(developmentTrack);
		}

	}

	@POST @Path("/tracks/{trackId}/updateWeight")
	@Consumes("application/x-www-form-urlencoded")
	public void updateWeight(
			@PathParam("trackId") String developmentTrackId, 
			@FormParam("weight") float weight, 
			@FormParam("pounds") boolean pounds) {
		DevelopmentTrack developmentTrack = developmentTrackDAO.findByPrimaryKey(developmentTrackId, false);
		if (developmentTrack != null) {
			developmentTrack.setWeight(new Weight(weight, pounds));
			developmentTrackDAO.save(developmentTrack);
		}

	}

}
